package com.xigege.netty.simpleExample.basic;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

/**
 * @author huangxi
 * @class HelloClient
 * @description HelloClient
 * @create 2022-08-19 15:43
 */
public class HelloClient {
    public static void main(String[] args) throws InterruptedException {
        new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel sc) throws Exception {
                        sc.pipeline().addLast(new StringEncoder());
                    }
                }).connect(new InetSocketAddress("localhost", 6666))
                // 是一个阻塞方法，连接建立后才会往下运行
                .sync()
                .channel()
                // 向服务器发送数据
                .writeAndFlush("hello world");
    }
}
